<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <!-- <el-col :span="24">
          <el-form-item label="所属应用" prop="appId">
            <el-select v-model="dataForm.appId" filterable placeholder="所属应用" style="width:100%" clearable>
              <el-option v-for="app in appList" :key="app.id" :label="app.appName" :value="app.appId"></el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <!-- 模板编号 -->
          <el-form-item label="模板编号" prop="templateId">
            <!--文本框-->
            <el-input v-model="dataForm.templateId" placeholder="模板编号" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 模板名称 -->
          <el-form-item label="模板名称" prop="templateName">
            <!--文本框-->
            <el-input v-model="dataForm.templateName" placeholder="模板名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 签名码 -->
          <el-form-item :label="$t('msgSmsTemplate.signatureCode')" prop="signatureCode">
            <!--文本框-->
            <el-select v-model="dataForm.signatureCode" filterable :placeholder="$t('msgSmsTemplate.signatureCode')" style="width:100%" clearable>
              <el-option v-for="signature in signatureList" :key="signature.id" :label="signature.signatureText" :value="signature.signatureCode"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 正文模板 -->
          <el-form-item label="正文模板" prop="content">
            <!--文本域-->
            <el-input type="textarea" :rows="3" v-model="dataForm.content" placeholder="正文模板"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="说明">
            <span>type 见字典数据 SYS_MSG_TYPE 的键值 如：SYS_MSG_PRO</span> <br />
            <span>level 1重要2一般3低 </span> <br />
            <span>title-标题、content-消息内容 适合组合话术</span>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      appList: [],
      signatureList: [],
      dataForm: {
        id: '',
        appId: 'EDP',
        signatureCode: '',
        templateId: '',
        templateName: '',
        /* eslint-disable */
        content: '{"title":"${title}","content":"${content}","type":"SYS_MSG_PRO","level":"2","url":""}',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        appId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        signatureCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        templateId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
        // this.getAppList()
        this.getSignatureList()
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/msg/msgRecordTemplate/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/msg/msgRecordTemplate/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    // 获取APP
    // getAppList () {
    //   return this.$http.get('/sys/sysApp/list').then(({ data: res }) => {
    //     if (res.code !== 0) {
    //       return this.$message.error(res.msg)
    //     }
    //     this.appList = res.data
    //   }).catch(() => { })
    // },
    // 获取签名
    getSignatureList () {
      return this.$http.get('/msg/msgSignature/list').then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.signatureList = res.data
      }).catch(() => { })
    }
  }
}
</script>
